package com.automannn.atm_back.modules.codeGenerator.service.impl;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.TemplateEntity;
import com.automannn.atm_back.modules.codeGenerator.mapper.TemplateEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.service.TemplateEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* (GeneratorTemplate)表服务实现类
*
* @author chenkaihai
* @since 2020-07-12 17:02:41
*/
@Service
public class TemplateEntityServiceImpl implements TemplateEntityService {
    @Resource
    private TemplateEntityMapper templateEntityMapper;


    /**
    * 查询一个对象
    *
    * @param  id
    * return 实例对象
    */
    @Override
    public TemplateEntity queryOne(String id) {
        return this.templateEntityMapper.queryById(id);
    }

    /**
    * 查询所有对象
    *
    * return 实例对象
    */
    @Override
    public IPage<TemplateEntity> queryAll(Page<TemplateEntity> page, TemplateEntity generatorTemplate) {
        return this.templateEntityMapper.queryAll(page,generatorTemplate);
    }

    /**
    * 新增数据
    *
    * @param generatorTemplate 实例对象
    * @return 实例对象
    */
    @Override
    public TemplateEntity insert(TemplateEntity generatorTemplate) {
        this.templateEntityMapper.insert(generatorTemplate);
        return generatorTemplate;
    }

    /**
    * 修改数据
    *
    * @param generatorTemplate 实例对象
    * @return 实例对象
    */
    @Override
    public void update(TemplateEntity generatorTemplate) {
        templateEntityMapper.update(generatorTemplate);
    }

    /**
    * 通过主键删除数据
    *
    * @param id 主键
    * @return 是否成功
    */
    @Override
    public boolean deleteById(String id) {
        return this.templateEntityMapper.deleteById(id) > 0;
    }

    @Override
    public void deleteBatchByIds(String ids) {
        templateEntityMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public void saveOrUpdate(List<TemplateEntity> templateEntityList) {
        List<TemplateEntity>   toUpdateList=
                templateEntityList.stream().filter(templateEntity -> templateEntity.getId() != null && templateEntity.getId() != "").collect(Collectors.toList());
        List<TemplateEntity> toAddList =
                templateEntityList.stream().filter(templateEntity -> templateEntity.getId() == null || templateEntity.getId() == "").collect(Collectors.toList());
        toAddList.forEach(columnConfigEntity -> templateEntityMapper.insert(columnConfigEntity));
        toUpdateList.forEach(
                columnConfigEntity -> templateEntityMapper.updateById(columnConfigEntity));

    }
}
